﻿@using Citi.Scheduler.WebUi.Models
@using Webdiyer.WebControls.Mvc
@model Webdiyer.WebControls.Mvc.PagedList<LibraryVersionModel>
<link href="@Url.Content("~/Content/uploadify.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery.uploadify.min.js")" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        $('.imageStatus').tooltip();
        $('#divUpload').dialog({ autoOpen: false, modal: true });
    });

    function showHideDetails(id) {
        $('.trVersionDetails').hide();

        if ($('#versionShowDetail' + id).is(":visible"))
            $('#versionDetail' + id).show();

        $('#versionShowDetail' + id).toggle();
        $('#versionHideDetail' + id).toggle();
    }

    function openUploadLibraryDialog() {
        $('#divUpload').dialog('open');
    }
</script>
<table style="width: 100%">
    <tr>
        <th>
            Version
        </th>
        <th style="width: 90px;">
            Status
        </th>
        <th>
            Type
        </th>
        <th>
            LibraryFile
        </th>
        <th>
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @{
        var errorMessage = string.Empty;

        switch (item.Status)
        {
            case Enums.LibraryStatus.Error:
                errorMessage = "Were found some errors in the uploaded library.";
                break;
            case Enums.LibraryStatus.InvalidDll:
                errorMessage = "The DLL file is invalid. The library must have at least one class implementing IExecutableRoutine.";
                break;
            case Enums.LibraryStatus.NoFile:
                errorMessage = "No file uploaded.";
                break;
            case Enums.LibraryStatus.UnderCorrection:
                errorMessage = "Under correction in development team.";
                break;
        }

        if (item.Status != Enums.LibraryStatus.Ok)
        {
                    <img class="imageStatus" src="@Url.Content("~/Content/Images/ExclamationError.png")" title="@errorMessage" style="width: 20px;"/>
        }
        else
        {
                    <img class="imageStatus" src="@Url.Content("~/Content/Images/Check.png")" title="There's no error." style="width: 20px;"/>
        }
                }
                @Html.DisplayFor(modelItem => item.Version) - @Html.DisplayFor(modelItem => item.Type)
            </td>
            <td>
                @item.Status.GetDescription()
            </td>
            <td>
                @item.Type.GetDescription()
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.LibraryFile)
                @if (item.Status == Enums.LibraryStatus.NoFile)
                {
                    @Ajax.ActionLink("Upload", "Upload", "LibraryVersion", new { id = item.Id }, new AjaxOptions { UpdateTargetId = "divUpload", OnBegin = "openUploadLibraryDialog" }, new { @class = "" })
                }
            </td>
            <td>
                <a href="#" id="@string.Concat("versionShowDetail", item.Id)" onclick="showHideDetails(@item.Id);">
                    View details</a> <a href="#" id="@string.Concat("versionHideDetail", item.Id)" style="display: none;" onclick="showHideDetails(@item.Id);">
                        Hide details</a>
            </td>
        </tr>
        <tr style="display: none;" id="@string.Concat("versionDetail", item.Id)" class="trVersionDetails">
            <td>
                @Html.DisplayFor(modelItem => item.User.Name)
            </td>
            <td>
                @item.Date.ToString("yyyy/MM/dd HH:mm")
            </td>
            <td colspan="3">
                <strong>Version notes:</strong>
                @Html.DisplayFor(modelItem => item.ChangeDescription)
            </td>
        </tr>
    }
</table>
<div style="text-align: center">
    @Html.AjaxPager(Model, new PagerOptions
        {
            PageIndexParameterName = "page",
            ShowDisabledPagerItems = false,
            AlwaysShowFirstLastPageNumber = true
        },
                    new AjaxOptions { UpdateTargetId = "divIndexLibraryHistory" })
</div>
<div id="divUpload" title="Upload">
</div>
